<template>
  <div class="app-container app-content" id="alarmParam">
    <el-row>
      <el-col :span="24">
        <el-row class="line-bottom" v-show="showSearch">
          <el-col class="form_col">
            <el-form :model="queryParams" ref="queryForm" :size="size" v-show="showSearch" :label-width="labelWidth">
              <div id="searchFilter" :gutter="10" style="display: flex; flex-wrap: wrap">
                <el-form-item label="策略名称" prop="strategyName">
                  <el-input
                      v-model="queryParams.strategyName"
                      placeholder="请输入策略名称"
                      clearable
                      @keyup.enter.native="handleQuery"
                      style="width:100%;"
                  />
                </el-form-item>
                <!-- <el-form-item label="报警对象类型" prop="targetType">
                  <el-select v-model="queryParams.targetType" placeholder="请选择报警对象类型" clearable style="width:100%;">
                    <el-option
                      v-for="dict in dict.type.warntargettype"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item label="报警参数类型" prop="paramType">
                  <el-select v-model="queryParams.paramType" placeholder="请选择报警参数类型" clearable style="width:100%;">
                    <el-option
                      v-for="dict in dict.type.warnparamtype"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item> -->
                <el-form-item label="是否启用" prop="enable">
                  <el-select v-model="queryParams.enable" placeholder="请选择是否启用" clearable style="width:100%;">
                    <el-option
                        v-for="dict in dict.type.yes_no"
                        :key="dict.value"
                        :label="dict.label"
                        :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
                <el-form-item>
                </el-form-item>
              </div>
            </el-form>
          </el-col>
          <el-col class="form_query">
            <el-button v-show="collapseAble" type="text" @click="shiftCollapseAble" class="text">
              <span>
                {{ fold ? '收起筛选' : '更多筛选' }}
              </span>
            </el-button>
            <el-button type="primary" @click="handleQuery" size="mini">查询</el-button>
            <el-button @click="resetQuery" size="mini">重置</el-button>
          </el-col>
        </el-row>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['basedata:strategy:add']"
            >添加
            </el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['basedata:strategy:edit']"
            >修改</el-button>
          </el-col> -->
          <el-col :span="1.5">
            <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['basedata:strategy:remove']"
            >批量删除
            </el-button>
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['basedata:strategy:export']"
            >导出</el-button>
          </el-col> -->
          <!-- <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar> -->
          <RightDropdown :columns="columns" :showSearch.sync="showSearch" @queryTable="getList" @export="handleExport"
                         :hasPermiExport="['basedata:strategy:export']"></RightDropdown>
        </el-row>

        <el-table v-loading="loading" :data="strategyList" border size="mini"
                  @selection-change="handleSelectionChange" ref="table" :height="getAvailableHeight">
          <el-table-column type="selection" width="55" align="center"/>
          <!-- <el-table-column label="平台自增主键" align="center" prop="id"  width="120px" v-if="false" :show-overflow-tooltip="true"/> -->
          <!-- <el-table-column label="所属租户ID" align="center" prop="tenantId" width="120px" v-if="false" :show-overflow-tooltip="true"/> -->
          <el-table-column label="策略名称" align="center" prop="strategyName" v-if="columns[0].visible"
                           :show-overflow-tooltip="true"/>
          <!-- <el-table-column label="报警对象类型" align="center" prop="targetType" width="120px" v-if="columns[1].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.warntargettype" :value="scope.row.targetType"/>
            </template>
          </el-table-column>
          <el-table-column label="报警参数类型" align="center" prop="paramType" width="120px" v-if="columns[2].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.warnparamtype" :value="scope.row.paramType"/>
            </template>
          </el-table-column>
          <el-table-column label="是否通用" align="center" prop="isGeneral" v-if="columns[3].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.yes_no" :value="scope.row.isGeneral"/>
            </template>
          </el-table-column>
          <el-table-column label="报警对象编号" align="center" prop="terminalId" v-if="columns[4].visible" :show-overflow-tooltip="true" width="120"/>
          <el-table-column label="起始时间" align="center" prop="startTime" v-if="columns[5].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.startTime, '{h}-{i}-{s}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="截止时间" align="center" prop="endTime" v-if="columns[6].visible" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.endTime, '{h}-{i}-{s}') }}</span>
            </template>
          </el-table-column> -->
          <el-table-column label="一级上限限制值" align="center" prop="level1Up" v-if="columns[1].visible"
                           :show-overflow-tooltip="true"/>
          <el-table-column label="一级下限限制值" align="center" prop="level1Down" v-if="columns[2].visible"
                           :show-overflow-tooltip="true"/>
          <!-- <el-table-column label="二级上限限制值" align="center" prop="Level2Up" width="120px" v-if="columns[9].visible" :show-overflow-tooltip="true"/>
          <el-table-column label="三级上限限制值" align="center" prop="Level3Up" width="120px" v-if="columns[10].visible" :show-overflow-tooltip="true"/> -->
          <!-- ，0：否，1：是，默认1 -->
          <el-table-column label="是否启用" align="center" prop="enable" v-if="columns[3].visible"
                           :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.yes_no" :value="scope.row.enable"/>
            </template>
          </el-table-column>
          <!-- <el-table-column label="备注" align="center" prop="Remark" v-if="columns[12].visible" :show-overflow-tooltip="true"/> -->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120px"
                           fixed="right">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  v-hasPermi="['basedata:strategy:edit']"
                  class="text_color"
              >修改
              </el-button>
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                  v-hasPermi="['basedata:strategy:remove']"
                  class="text_color"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
      </el-col>
    </el-row>
    <!-- 添加或修改告警策略对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="740px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px" :inline="true">
        <el-form-item label="归属水司" prop="tenantId">
          <el-select v-model="form.tenantId" placeholder="请选择归属水司" style="width: 202px;">
            <el-option
                v-for="dict in dict.type.belong_department"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value.toString()"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="策略名称" prop="strategyName">
          <el-input v-model="form.strategyName" placeholder="请输入策略名称"/>
        </el-form-item>
        <el-form-item label="报警对象类型" prop="targetType">
          <el-select v-model="form.targetType" placeholder="请选择报警对象类型" style="width: 202px;">
            <el-option
                v-for="dict in dict.type.warntargettype"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报警参数类型" prop="paramType">
          <el-select v-model="form.paramType" placeholder="请选择报警参数类型" style="width: 202px;">
            <el-option
                v-for="dict in dict.type.warnparamtype"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否通用" prop="isGeneral">
          <el-select v-model="form.isGeneral" placeholder="请选择是否通用" style="width: 202px;">
            <el-option
                v-for="dict in dict.type.yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="报警对象编号" prop="TargetIDTerminalID">
          <el-input v-model="form.terminalId" placeholder="请输入报警对象编号"/>
        </el-form-item>
        <!-- <el-form-item label="起始时间" prop="startTime">
          <el-time-picker clearable
            v-model="form.startTime"
            value-format="HH:mm:ss"
            placeholder="请选择起始时间"
            style="width: 202px;">
          </el-time-picker>
        </el-form-item>
        <el-form-item label="截止时间" prop="endTime">
          <el-time-picker clearable
            v-model="form.endTime"
            value-format="HH:mm:ss"
            placeholder="请选择截止时间"
            style="width: 202px;">
          </el-time-picker>
        </el-form-item> -->
        <el-form-item label="一级上限限制值" prop="level1Up">
          <el-input v-model="form.level1Up" placeholder="请输入一级上限限制值"/>
        </el-form-item>
        <el-form-item label="一级下限限制值" prop="level1Down">
          <el-input v-model="form.level1Down" placeholder="请输入一级下限限制值"/>
        </el-form-item>
        <!-- <el-form-item label="二级上限限制值" prop="Level2Up">
          <el-input v-model="form.Level2Up" placeholder="请输入二级上限限制值" />
        </el-form-item>
        <el-form-item label="三级上限限制值" prop="Level3Up">
          <el-input v-model="form.Level3Up" placeholder="请输入三级上限限制值" />
        </el-form-item> -->
        <el-form-item label="是否启用" prop="enable">
          <el-select v-model="form.enable" placeholder="请选择是否启用" style="width: 202px;">
            <el-option
                v-for="dict in dict.type.yes_no"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="Remark">
          <el-input v-model="form.Remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listStrategy, getStrategy, delStrategy, addStrategy, updateStrategy} from "@/api/basedata/alarmparam";

export default {
  name: "AlarmParam",
  dicts: ['warnparamtype', 'warntargettype', 'yes_no', 'belong_department'],
  data() {
    return {
      screenHeight: null,
      size: 'mini',
      collapseAble: false,
      fold: false,
      maxShow: '4',
      labelWidth: '100px',
      // 列信息
      columns: [
        // { key: 0, label: `策略名称`, visible: true },
        // { key: 1, label: `报警对象类型`, visible: true },
        {key: 0, label: `策略名称`, visible: true},
        // { key: 1, label: `报警对象类型`, visible: true },
        // { key: 2, label: `报警参数类型`, visible: true },
        // { key: 3, label: `是否通用`, visible: true },
        // { key: 4, label: `层级ID`, visible: true },
        // { key: 5, label: `起始时间`, visible: true },
        // { key: 6, label: `截止时间`, visible: true },
        {key: 2, label: `一级上限限制值`, visible: true},
        {key: 3, label: `一级下限限制值`, visible: true},
        // { key: 9, label: `二级上限限制值`, visible: true },
        // { key: 10, label: `三级上限限制值`, visible: true },
        {key: 11, label: `是否启用`, visible: true},
        // { key: 12, label: `备注`, visible: true }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 告警策略表格数据
      strategyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        // targetType: null,
        // paramType: null,
        strategyName: null,
        enable: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tenantId: [
          {required: true, message: "归属水司不能为空", trigger: "change"}
        ],
        strategyName: [
          {required: true, message: "策略名称不能为空", trigger: "blur"}
        ],
        targetType: [
          {required: true, message: "报警对象类型", trigger: "change"}
        ],
        paramType: [
          {required: true, message: "报警参数类型", trigger: "change"}
        ],
        isGeneral: [
          {required: true, message: "是否通用", trigger: "change"}
        ],
        // startTime: [
        //   { required: true, message: "起始时间不能为空", trigger: "blur" }
        // ],
        // endTime: [
        //   { required: true, message: "截止时间不能为空", trigger: "blur" }
        // ],
      }
    };
  },
  created() {
    this.getList();
    this.screenHeight = document.body.clientHeight
  },
  computed: {
    getAvailableHeight() {
      return this.screenHeight - (this.showSearch ? 230 : 173)
    },
  },
  methods: {
    // 展开折叠按钮
    shiftCollapseAble() {
      this.fold = !this.fold
      this.minShowControl()
    },
    // 通过maxShow控制元素显示/折叠
    minShowControl() {
      const group = window.document.querySelectorAll(`#searchFilter .el-form-item.el-form-item--${this.size}`)
      const len = group?.length ? group?.length - 1 : 0
      if (this.maxShow < len) {
        group.forEach((item, index) => {
          if (index > this.maxShow - 1 && index < len) {
            item.hidden = !this.fold
          }
        })
        this.collapseAble = true
      } else {
        this.collapseAble = false
      }
    },
    /** 查询告警策略列表 */
    getList() {
      this.loading = true;
      listStrategy(this.queryParams).then(response => {
        this.strategyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tenantId: null,
        strategyName: null,
        targetType: null,
        paramType: null,
        isGeneral: null,
        terminalId: null,
        // startTime: null,
        // endTime: null,
        level1Up: null,
        level1Down: null,
        // Level2Up: null,
        // Level3Up: null,
        enable: null,
        // Remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报警策略";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStrategy(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报警策略";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStrategy(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStrategy(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const IDs = row.id || this.ids;
      this.$modal.confirm('是否确认删除报警策略编号为"' + IDs + '"的数据项？').then(function () {
        return delStrategy(IDs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('basedata/strategy/export', {
        ...this.queryParams
      }, `strategy_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.form_col {
  width: 80%;

  ::v-deep .el-form {
    width: 100%;
  }

  ::v-deep .el-form-item {
    width: 25%;
  }
}

.form_query {
  width: 20%;
  text-align: right;
}
</style>
